﻿package com.dnedelcu.robattle{
	
	import flash.events.Event;
    import flash.events.EventDispatcher;
	import com.dnedelcu.robattle.Monster;
	import com.dnedelcu.robattle.GetStatsEvent;
	
	public class Player extends EventDispatcher{
		
		private var _characterName:String;
		private var _class:String;
		private var _characterLevel:int;
		private var _characterStatPoints:int;
		private var _currentLife:int;
		private var _maximumLife:int;
		private var _currentMana:int;
		private var _maximumMana:int;
		private var _armor:int;
		private var _minimumAttack:int;
		private var _maximumAttack:int;
		private var _currentXp:int;
		private var _maximumXp:int;
		private var _gold:int;
		private var _percentHp:Number;
		private var _percentMana:Number;
		private var _damageReduction:Number;
		private var _userItemDrops:Array;
		
		public function Player (userInfo:Object) {
			_characterName = userInfo['name'];
			_class = userInfo['class'];
			_characterLevel = userInfo['level'];
			_characterStatPoints = userInfo['stat_points'];
			_currentLife = userInfo['curlife'];
			_maximumLife = userInfo['maxlife'];
			_currentMana = userInfo['curmana'];
			_maximumMana = userInfo['maxmana'];
			_armor = userInfo['armor'];
			_minimumAttack = userInfo['minattack'];
			_maximumAttack = userInfo['maxattack'];
			_currentXp = userInfo['curxp'];
			_maximumXp = userInfo['maxxp'];
			_gold = userInfo['gold'];
			_percentHp = _currentLife / _maximumLife;
			_percentMana = _currentMana / _maximumMana;
			_damageReduction = _armor / (_armor + 400 + 85 * _characterLevel);
			_userItemDrops = new Array();
		}
		
		public function getRandom(lowNumber, highNumber):int {
			var thisNumber = highNumber - lowNumber;
			var randomUnrounded = Math.random() * thisNumber;
			var randomNumber = Math.round(randomUnrounded);
			randomNumber += lowNumber;
			return randomNumber;
		}
		
		public function Attack(_monster:Monster):void {
			var rand:int = getRandom(minimumAttack, maximumAttack);
			var damageReduced = Math.round(rand * _monster.damageReduction);
			var damage = rand - damageReduced;
			_monster.currentLife = -damage;
			if(_monster.currentLife <= 0) {
				_monster.currentLife = -_monster.currentLife;
				_monster.updatePercentHp();
				_monster.updatePercentMana();
				var e1:GetStatsEvent = new GetStatsEvent(GetStatsEvent.MONSTER_KILLED, true,true);
				dispatchEvent(e1);
			} else {
				_monster.updatePercentHp();
				_monster.updatePercentMana();
				var e2:GetStatsEvent = new GetStatsEvent(GetStatsEvent.USER_ATTACKED, true, true);
				dispatchEvent(e2);
			}
		}
				
		public function get characterName():String {
			return _characterName;
		}

		public function get characterClass():String {
			return _class;
		}
		
		public function get characterLevel():int {
			return _characterLevel;
		}
		
		public function get characterStatPoints():int {
			return _characterStatPoints;
		}
		
		public function get currentLife():int {
			return _currentLife;
		}
		
		public function get maximumLife():int {
			return _maximumLife;
		}
		
		public function get currentMana():int {
			return _currentMana;
		}
		
		public function get maximumMana():int {
			return _maximumMana;
		}
		
		public function get armor():int {
			return _armor;
		}
		
		public function get minimumAttack():int {
			return _minimumAttack;
		}

		public function get maximumAttack():int {
			return _maximumAttack;
		}
		
		public function get currentXp():int {
			return _currentXp;
		}
		
		public function get maximumXp():int {
			return _maximumXp;
		}
		
		public function get gold():int {
			return _gold;
		}
		
		public function get percentHp():Number {
			return _percentHp;
		}
		
		public function get percentMana():Number {
			return _percentMana;
		}
		
		public function get damageReduction():Number {
			return _damageReduction;
		}
		
		public function get userItemDrops():Array {
			return _userItemDrops;
		}
		
		public function get userInfo():Array {
			var userInfo:Array = new Array;
			userInfo['level'] = _characterLevel;
			userInfo['stat_points'] = _characterStatPoints;
			userInfo['curlife'] = _currentLife;
			userInfo['maxlife'] = _maximumLife;
			userInfo['curmana'] = _currentMana;
			userInfo['maxmana'] = _maximumMana;
			userInfo['armor'] = _armor;
			userInfo['minattack'] = _minimumAttack;
			userInfo['maxattack'] = _maximumAttack;
			userInfo['curxp'] = _currentXp;
			userInfo['gold'] = _gold;
			userInfo['itemDrops'] = _userItemDrops;
			return userInfo;
		}
		
		public function getUserItemDrops(index:int):Array {
			return _userItemDrops[index];
		}
		
		public function set characterLevel(level_:int):void {
			_characterLevel += level_;
		}
		
		public function set characterStatPoints(statPoints_:int):void {
			_characterStatPoints += statPoints_;
		}
		
		public function set currentLife(life_:int):void {
			_currentLife += life_;
		}
		public function set maximumLife(life_:int):void {
			_maximumLife += life_;
		}
		
		public function set currentMana(mana_:int):void {
			_currentMana += mana_;
		}
		
		public function set maximumMana(mana_:int):void {
			_maximumMana += mana_;
		}
		
		public function set armor(armor_:int):void {
			_armor += armor_;
		}
		
		public function set minimumAttack(attack_:int):void {
			_minimumAttack += attack_;
		}
		
		public function set maximumAttack(attack_:int):void {
			_maximumAttack += attack_;
		}
		
		public function set currentXp(xp_:int):void {
			_currentXp += xp_;
		}
		
		public function set gold(gold_:int):void {
			_gold += gold_;
		}
		
		public function set userItemDrops(userItemDrops_:Array):void {
			_userItemDrops = userItemDrops_;
		}
		
		public function updatePercentHp():void {
			_percentHp = _currentLife / _maximumLife;
		}
		
		public function updatePercentMana():void {
			_percentMana = _currentMana / _maximumMana;
		}
		
		public function setDamageReduction():void {
			_damageReduction = _armor / (_armor + 400 + 85 * _characterLevel);
		}
	}
}